<?php
namespace App\DBs\Admin;

use Illuminate\Support\Facades\DB;
use App\DBs\BaseDB as Base;

class TongjiDB extends Base
{
    //管理员

    public function __construct()
    {
    }

    //主页统计
    public function getTongji()
    {
        $res = DB::table($this->tClient)->orderBy('id','desc')->skip(0)->take(10)->get();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $items = [];
        foreach ($res as $re) {
            $items[] = [
                'id'=>$re['id'], 'req'=>$re['client'], 'addtime'=>$re['addtime'],
            ];
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //学术列表
    public function getLearnIndex()
    {
        $db = DB::table($this->tLearn)->where('del',0);
        $res = $db->orderBy('id','desc')->get();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $items = [];
        foreach ($res as $re) {
            $img = env('DOMAIN').'/'.$re['img'];
            $detail = $re['detail'] ? json_decode($re['detail'],true) : '';
            $items[] = [
                'id'=>$re['id'], 'title'=>$re['title'], 'detail'=>$detail, 'img'=>$img,
            ];
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //学术一个
    public function getLearnOne($id)
    {
        $res = DB::table($this->tLearn)->where('id',$id)->first();
        if (!$res) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $img = env('DOMAIN').'/'.$res['img'];
        $detail = $res['detail'] ? json_decode($res['detail'],true) : '';
        $items = [
            'id'=>$res['id'], 'title'=>$res['title'], 'detail'=>$detail, 'img'=>$img,
        ];
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //学术新增
    public function setLearnAdd($arys)
    {
        $totime = date('Y-m-d H:i:s',time());
        $arys['addtime'] = $totime;
        $res = DB::table($this->tLearn)->insertGetId($arys);
        if (!$res) {
            return array('code'=>-2,'msg'=>'新增错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
    }

    //学术修改
    public function setLearnUpdate($id,$arys)
    {
        $totime = date('Y-m-d H:i:s',time());
        $arys['addtime'] = $totime;
        $res = DB::table($this->tLearn)->where('id',$id)->update($arys);
        if (!$res) {
            return array('code'=>-2,'msg'=>'新增错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
    }

    //学术删除
    public function setLearnDel($id)
    {
        $totime = date('Y-m-d H:i:s',time());
        $res = DB::table($this->tLearn)->where('id',$id)->update(['del'=>1,'deltime'=>$totime]);
        if (!$res) {
            return array('code'=>-2,'msg'=>'删除错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
    }

    //影像馆列表
    public function getYxgIndex()
    {
        $db = DB::table($this->tImgs)->where('del',0)->where('type',3);
        $res = $db->orderBy('id','desc')->get();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $items = [];
        foreach ($res as $re) {
//            if (substr($re['img'],0,10)=='data:image') {
//                $img = $re['img'];
//            } else {
//                $img = env('DOMAIN').'/'.$re['img'];
//            }
            $img = env('DOMAIN').'/'.$re['img'];
            $items[] = [
                'id'=>$re['id'], 'img'=>$img,
            ];
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //影像馆新增
    public function setYxgAdd($arys)
    {
        $totime = date('Y-m-d H:i:s',time());
        $arys['addtime'] = $totime;
        $res = DB::table($this->tImgs)->insertGetId($arys);
        if (!$res) {
            return array('code'=>-2,'msg'=>'新增错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
    }

    //影像馆删除
    public function setYxgDel($id)
    {
        $totime = date('Y-m-d H:i:s',time());
        $res = DB::table($this->tImgs)->where('id',$id)->update(['del'=>1,'deltime'=>$totime]);
        if (!$res) {
            return array('code'=>-2,'msg'=>'删除错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
    }
}